package com.tbs.easyhomemoneyserver.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.tbs.easyhomemoneyserver.DO.ApiLimit;
import com.tbs.easyhomemoneyserver.DO.LoginInfo;
import com.tbs.easyhomemoneyserver.constants.CommonConstants;
import com.tbs.easyhomemoneyserver.constants.LoginInfoConstants;
import com.tbs.easyhomemoneyserver.dao.ApiLimitDao;
import com.tbs.easyhomemoneyserver.interfaces.IRightAuthorization;
import com.tbs.easyhomemoneyserver.model.ApiRequest;
import com.tbs.easyhomemoneyserver.model.ApiRightDTO;
import com.tbs.easyhomemoneyserver.service.IApiLimitService;
import com.tbs.easyhomemoneyserver.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

@Service
public class ApiRightsServiceImpl implements IApiLimitService {

    @Resource
    ApiLimitDao apiLimitDao;


    ThreadLocal<ApiRequest> apiRequestThreadLocal = new InheritableThreadLocal<>();

    @Resource
    IUserService userService;



    @Override
    @Cacheable(cacheNames = "api", key = "#url")
    public ApiRightDTO getEnabledRights(String url) {
        List<ApiLimit> limits = apiLimitDao.findAllByUrlAndEnable(url, CommonConstants.ENABLE);
        ApiRightDTO rightDTO = new ApiRightDTO(url);
        for (ApiLimit limit : limits) {
            if (limit == null) {
                continue;
            }
            Map<String, ApiLimit> mp = rightDTO.getRightsMap().getOrDefault(limit.getRoleType(), new HashMap<>());
            mp.put(limit.getRole(), limit);
            rightDTO.getRightsMap().put(limit.getRoleType(), mp);
        }
        return rightDTO;
    }

    @Override
    @Cacheable(cacheNames = "userFeature", key = "#userid")
    public Map<Integer, Set<String>> findUserRights(Long userid) {
        return new HashMap<>();//fixme 实现根据用户id获取用户的功能权限
    }

    @Override
    public IRightAuthorization getAuthMethod(ApiLimit limit) {
        return SpringUtil.getBean(limit.getAuthType());
    }

    @Override
    public ApiRequest getLoginInfo(String loginId) {
        if (apiRequestThreadLocal.get() != null) {
            return apiRequestThreadLocal.get();
        }
        ApiRequest apiRequest = new ApiRequest();
        apiRequest.setLogin(true);
        if (StringUtils.isEmpty(loginId)) {
            apiRequest.setLogin(false);
            return apiRequest;
        }
        LoginInfo loginInfo = userService.getLoginInfoByID(Long.valueOf(loginId));
        if (loginInfo == null || !LoginInfoConstants.LOGIN.equals(loginInfo.getLoginFlag())) {
            apiRequest.setLogin(false);
            return apiRequest;
        }
        apiRequest.setUserInfo(userService.getUserById(loginInfo.getUserId()));
        apiRequestThreadLocal.set(apiRequest);
        return apiRequest;
    }
}
